/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-14
 * Time: 18:07
 */
public class MyQueue {
    static class ListNode {
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    private ListNode head;
    private ListNode last;
    private int size;

    //入队列
    public boolean offer(int data) {
        ListNode cur = new ListNode(data);
        if (this.head == null) {
            this.head = this.last = cur;
        }else {
            this.last.next = cur;
            this.last = cur;
        }
        this.size++;
        return true;
    }

    //出队列
    public Integer poll() {
        if (isEmpty()) {
            return null;
        }
        int val = this.head.val;
        this.head = this.head.next;
        this.size--;
        return val;
    }

    //获取队头元素
    public Integer peek() {
        if (isEmpty()) {
            return null;
        }
        return this.head.val;
    }

    //获取队列中有效元素个数
    public int size() {
        return this.size;
    }

    //检测队列是否为空
    public boolean isEmpty() {
        return this.head == null;
    }
}
